using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;
using System.Data.Common;
using System.Text;

namespace JNPF.Basics.Logic
{
    public class jiushiCustomerconsultantdetailService : DataFactory, jiushiCustomerconsultantdetailIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomerconsultantdetailEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerconsultantdetailEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerconsultantdetailEntity>> GetPageList(jiushiCustomerconsultantdetailListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerconsultantdetailEntity>();
                var queryParam = input.queryJson.ToObject();
                ////查询关联主表UUID
                //if (!string.IsNullOrEmpty(input.uuid))
                //{
                //	queryWhere = queryWhere.And(m => m.Uuid.Contains(input.uuid));
                //}
                ////查询金额
                //if (input.amount.ToDecimal()>0)
                //{
                //queryWhere = queryWhere.And(m => m.Amount == input.amount);
                //}
                ////查询合伙人人ID
                //if (!string.IsNullOrEmpty(input.partnerUserId))
                //{
                //	queryWhere = queryWhere.And(m => m.PartnerUserId.Contains(input.partnerUserId));
                //}
                ////查询合伙人积分
                //if (input.partnerPoint.ToDecimal()>0)
                //{
                //queryWhere = queryWhere.And(m => m.PartnerPoint == input.partnerPoint);
                //}
                ////查询上级合伙人ID
                //if (!string.IsNullOrEmpty(input.parentPartnerUserId))
                //{
                //	queryWhere = queryWhere.And(m => m.ParentPartnerUserId.Contains(input.parentPartnerUserId));
                //}
                ////查询上级合伙人抽佣积分
                //if (input.parentUserPoint.ToDecimal()>0)
                //{
                //queryWhere = queryWhere.And(m => m.ParentUserPoint == input.parentUserPoint);
                //}
                return await db.GetIQueryable<CustomerconsultantdetailEntity>().Where(queryWhere).GetPageResultAsync(input);
            }
        }



        /// <summary>
        /// 列表（我收到的）
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<PageResult<jiushiCustomerconsultantdetailListOutput>> GetCusPageList(jiushiCustomerconsultantdetailListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	detail.F_ID,
	detail.F_UUID,
	detail.F_Amount,
	detail.F_PartnerUserId,
	detail.F_PartnerPoint,
	detail.F_ParentPartnerUserId,
	detail.F_ParentUserPoint,
	detail.F_SortCode,
	detail.F_EnabledMark,
	detail.F_CreatorTime,
	detail.F_CreatorUser,
	detail.F_CreatorUserId,
	detail.F_LastModifyTime,
	detail.F_LastModifyUserId,
	detail.F_DeleteMark,
	detail.F_DeleteTime,
	detail.F_DeleteUserId,
	detail.F_UserId,
	detail.F_Type,
	detail.F_ATTACHMENTINFO,
	base.F_CustomerName,
    uu.F_RealName UserName,
	ex.F_RealName partnerUserName,
	Parentex.F_RealName parentPartnerUserName,
	tant.F_ID tantId
FROM
	jiushi_customerconsultantdetail detail
	LEFT JOIN jiushi_customerconsultant tant ON detail.F_UUID = tant.F_UUID
	LEFT JOIN jiushi_customerbase base ON tant.F_CustomerId = base.F_ID
	LEFT JOIN base_user uu ON detail.F_userid = uu.F_ID
	LEFT JOIN base_user ex ON tant.F_PartnerUserId = ex.F_Id
	LEFT JOIN base_user Parentex ON tant.F_ParentPartnerUserId = Parentex.F_Id 
WHERE
	1 = 1");


                //关键词（消息标题）
                if (input.customerId.HasValue)
                {
                    dbSql.Append(" AND tant.F_CustomerId = @customerId");
                    parameters.Add(DataHelper.CreateParameter("@customerId", input.customerId));
                }
                if (!string.IsNullOrEmpty(input.uuid))
                {
                    dbSql.Append(" AND tant.F_UUID = @uuid");
                    parameters.Add(DataHelper.CreateParameter("@uuid", input.uuid));
                }
                //关键词（消息标题）
                if (!string.IsNullOrEmpty(input.customerName))
                {
                    dbSql.Append(" AND base.F_CustomerName like @customerName");
                    parameters.Add(DataHelper.CreateParameter("@customerName", '%' + input.customerName + '%'));
                }

                //关键词（消息标题）
                if (!string.IsNullOrEmpty(input.type))
                {
                    dbSql.Append(" AND detail.F_Type = @type");
                    parameters.Add(DataHelper.CreateParameter("@type", input.type.ToInt()));
                }


                //关键词（消息标题）
                if (!string.IsNullOrEmpty(input.partnerUserName))
                {
                    dbSql.Append(" AND ex.F_RealName like @partnerUserName");
                    parameters.Add(DataHelper.CreateParameter("@partnerUserName", '%' + input.partnerUserName + '%'));
                }


                //关键词（消息标题）
                if (!string.IsNullOrEmpty(input.parentPartnerUserName))
                {
                    dbSql.Append(" AND Parentex.F_RealNamelike @parentPartnerUserName");
                    parameters.Add(DataHelper.CreateParameter("@parentPartnerUserName", '%' + input.parentPartnerUserName + '%'));
                }

                if (!input.IsAdmin)
                {
                    if (input.systemType == 1)
                    {

                        dbSql.Append(" AND  base.F_MobilePhone =@MobilePhone  ");
                        parameters.Add(DataHelper.CreateParameter("@MobilePhone", UserProvider.Instance.Get().UserAccount));
                    }
                    else if (input.systemType == 2)
                    {
                        //信贷管家
                        dbSql.Append(" AND  detail.F_UserId =@userId  ");
                        parameters.Add(DataHelper.CreateParameter("@userId", UserProvider.Instance.Get().UserId));
                    }

                }
                input.sidx = "F_CreatorTime";


                return await PageHelper.GetPageResultAsync<jiushiCustomerconsultantdetailListOutput>(db, input, dbSql.ToString(), parameters.ToArray());
            }
        }



        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerconsultantdetailEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(CustomerconsultantdetailEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerconsultantdetailEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<CustomerconsultantdetailEntity>> GetList(jiushiCustomerconsultantdetailListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerconsultantdetailEntity>();
                ////查询关联主表UUID
                //if (!string.IsNullOrEmpty(input.uuid))
                //{
                //	queryWhere = queryWhere.And(m => m.Uuid.Contains(input.uuid));
                //}
                ////查询金额
                //if (input.amount.ToDecimal()>0)
                //{
                //queryWhere = queryWhere.And(m => m.Amount == input.amount);
                //}
                ////查询合伙人人ID
                //if (!string.IsNullOrEmpty(input.partnerUserId))
                //{
                //	queryWhere = queryWhere.And(m => m.PartnerUserId.Contains(input.partnerUserId));
                //}
                ////查询合伙人积分
                //if (input.partnerPoint.ToDecimal()>0)
                //{
                //queryWhere = queryWhere.And(m => m.PartnerPoint == input.partnerPoint);
                //}
                ////查询上级合伙人ID
                //if (!string.IsNullOrEmpty(input.parentPartnerUserId))
                //{
                //	queryWhere = queryWhere.And(m => m.ParentPartnerUserId.Contains(input.parentPartnerUserId));
                //}
                ////查询上级合伙人抽佣积分
                //if (input.parentUserPoint.ToDecimal()>0)
                //{
                //queryWhere = queryWhere.And(m => m.ParentUserPoint == input.parentUserPoint);
                //}
                return await db.GetIQueryable<CustomerconsultantdetailEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}